# Author Worms King

import random
import pygame

pygame.init()

class Game(object):

    def __init__(self, show_list, check = False):
        self.show_list = show_list
        self.check = check


    def show(self):     #显示列表
        for i in self.show_list:
            for j in i:

                print(j,end="\t")
            print("")

        print("-" * 20)


    def rand_num(self, check):      #随机产生2寻找空白位置放置
        self.check = check
        while self.check == False:
            x = random.randint(0,3)
            y = random.randint(0,3)
            if self.show_list[x][y] == 0:
                self.show_list[x][y] = 2
                self.check = True
            else:
                self.check = False

    def check_list(self,check):    #检查是否还有空位置
        self.check = check
        for i in self.show_list:
            for j in i:
                if j == 0:
                    self.check = True
                    return

        self.check = False
        return self.check



    def move_left(self):    #向左移动累加相同数值
        for k in range(0,3):
            for i in range(3, 0, -1):

                for j in range(0, 4):
                    #print(self.show_list[j][i])

                    if self.show_list[j][i] == self.show_list[j][i-1] or self.show_list[j][i] == 0:
                        self.show_list[j][i] += self.show_list[j][i-1]
                        self.show_list[j][i-1] = 0


    def move_right(self):   #向右移动累加相同数值
        for k in range(0, 3):
            for i in range(0, 3):

                for j in range(0, 4):
                    #print(self.show_list[j][i])

                    if self.show_list[j][i] == self.show_list[j][i+1] or self.show_list[j][i] == 0:
                        self.show_list[j][i] += self.show_list[j][i+1]
                        self.show_list[j][i+1] = 0


    def move_up(self):  #向上移动累加相同数值
        for k in range(0, 3):
            for i in range(3, 0, -1):

                for j in range(0, 4):
                    #print(self.show_list[i][j])

                    if self.show_list[i-1][j] == self.show_list[i][j] or self.show_list[i-1][j] == 0:
                        self.show_list[i-1][j] += self.show_list[i][j]
                        self.show_list[i][j] = 0


    def move_down(self):    #向下移动累加相同数值
        for k in range(0, 3):
            for i in range(2, -1, -1):

                for j in range(0, 4):
                    #print(self.show_list[i][j])

                    if self.show_list[i][j] == self.show_list[i+1][j] or self.show_list[i+1][j] == 0:
                        self.show_list[i+1][j] += self.show_list[i][j]
                        self.show_list[i][j] = 0


    def input_key(self):

        input_key = input("请输入移动的方向:(L,R,U,D)")

        if input_key == "L" or input_key == "l":
            game1.move_left()
            game1.show()
        elif input_key == "R" or input_key == "r":
            game1.move_right()
            game1.show()
        elif input_key == "U" or input_key == "u":
            game1.move_up()
            game1.show()
        elif input_key == "D" or input_key == "d":
            game1.move_down()
            game1.show()
        else:
            pass



show_list = [[0,0,2,2],[0,0,2,2],[0,2,2,0],[0,2,2,0]]
check = False   #检查是否该坐标已经有数字了
#add = 0    #检查是否有移动


game1 = Game(show_list)
game1.show()

input_key = None

while True:
    check = game1.rand_num(False)
    game1.show()
    check = game1.check_list(False) #检查列表中还有0 空白存在 如果不存在游戏卡死
    if check == False:
        print("游戏结束")
        exit()
    else:
        input_key = input("请输入移动的方向:(L,R,U,D)")


        if input_key == "L" or input_key == "l":
            game1.move_left()
            game1.show()
        elif input_key == "R" or input_key == "r":
            game1.move_right()
            game1.show()
        elif input_key == "U" or input_key == "u":
            game1.move_up()
            game1.show()
        elif input_key == "D" or input_key == "d":
            game1.move_down()
            game1.show()
        else:
            continue
